#!/usr/bin/env node
import fetch from 'node-fetch'
import process from 'process'

let args = process.argv.slice(2);
if (args.length == 0) {
    console.log("未指定搜索关键词.");
    process.exit(0)
}

let query = args[0]

console.log("GET - https://www.npmjs.com/search?q=" + query)
fetch("https://www.npmjs.com/search?q=" + query, {
  "headers": {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
    "manifest-hash": "1d7287b31927bbc348aa",
    "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-requested-with": "XMLHttpRequest",
    "x-spiferack": "1",
    "Referer": "https://www.npmjs.com/",
    "Referrer-Policy": "strict-origin-when-cross-origin"
  },
  "body": null,
  "method": "GET"
})
.then(response => response.json())
.then((response)=>{
    let objects = response["objects"]
    objects.forEach((object) => {
        let _package = object["package"]
        console.log(">>>", _package["name"] + "@" + _package["version"])
    })
});